﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading.Tasks;

using RexToy.Configuration;
using RexToy.Logging;

using Monitor.Core;

namespace Monitor.Service
{
    public partial class Service : ServiceBase
    {
        private static ILog _log = LogContext.GetLogger<Service>();
        private Scheduler _s;
        public Service()
        {
            InitializeComponent();
            RexToy.Runtime.SetStartupDirectory(System.Reflection.Assembly.GetAssembly(typeof(Service)));
            _log.Info("Set startup dir: " + RexToy.Runtime.StartupDirectory);
            AppConfig.Load(ConfigFactory.CreateXmlConfig());
            _s = new Scheduler();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                SensorController c = new SensorController();
                var sensors = c.LoadSensors();
                _s.Start();

                foreach (var sensor in sensors)
                    _s.PushSensor(sensor);
            }
            catch (Exception ex)
            {
                _log.Error(ex.ToString());
                throw;
            }
        }

        protected override void OnStop()
        {
            _s.Stop();
        }
    }
}
